var CsOrderCustomerServiceDatagrid = new HgUi.Datagrid("CsOrderCustomerService_datagrid");
CsOrderCustomerServiceDatagrid.toolbar = $("#CsOrderCustomerService_toolbar");
CsOrderCustomerServiceDatagrid.mode = "rowEdit"; //行编辑模式
CsOrderCustomerServiceDatagrid.options.isShowContextMenu = true;
CsOrderCustomerServiceDatagrid.options.onSelect = function(index,row) {
    if (row && row.dictGroupId){
        CsOrderCustomerServiceDatagrid.selectedRow = row;
    }
};
CsOrderCustomerServiceDatagrid.options.onLoadSuccess = function(data) {
    CsOrderCustomerServiceDatagrid.grid.datagrid("selectRow",0);
};
CsOrderCustomerServiceDatagrid.init();
CsOrderCustomerServiceDatagrid.grid.datagrid("enableFilter");
CsOrderCustomerServiceDatagrid.bindDefaultToolbarEvent();

// --------------------------保存数据------------------------------------------------------------------
CsOrderCustomerServiceDatagrid.toolbar.find("[tag=save]").click(function() {
    if (CsOrderCustomerServiceDatagrid.isEditing == true) {
        if (CsOrderCustomerServiceDatagrid.grid.datagrid("validateRow", CsOrderCustomerServiceDatagrid.editingIndex)) {
            var data = CsOrderCustomerServiceDatagrid.makeSubmitData();
            var editRow = CsOrderCustomerServiceDatagrid.grid.datagrid("getRows")[CsOrderCustomerServiceDatagrid.editingIndex];
            var submitUrl = "/cs/CsOrderCustomerService/add";
            if (editRow.id) {
                submitUrl = "/cs/CsOrderCustomerService/upd";
                data.id = editRow.id;
            }
            HgUtil.post(submitUrl, data, function(data) {
                if (!data.success) {
                    CsOrderCustomerServiceDatagrid.showMsgbox("error", data.data, 330);
                } else {
                    CsOrderCustomerServiceDatagrid.grid.datagrid('reload');
                }
            });

        } else {
            CsOrderCustomerServiceDatagrid.showMsgbox("alert", "有一条数据未校验通过!", 180);
        }
    }
});

//-------------------------删除-----------------------------------------------------------------------
CsOrderCustomerServiceDatagrid.toolbar.find("[tag=del]").click(function(){
    var row = CsOrderCustomerServiceDatagrid.grid.datagrid("getSelected");
    if (!row) {
        $.messager.alert("提示","请选择一条字典组数据","warning");
        return;
    }
    $.messager.confirm("确认删除", "确认将选中的字典组删除?", function(r){
        if (r) {
            HgUtil.getJson("/cs/CsOrderCustomerService/del",{id:row.id}, function(data){
                if (!data.success) {
                    $.messager.alert("提示","删除失败,"+data.data,"warning");
                } else {
                    HgUi.ok("删除成功!",function(){
                        CsOrderCustomerServiceDatagrid.grid.datagrid("reload");
                    });
                }
            });
        }
    });
});

// 组织提交数据
CsOrderCustomerServiceDatagrid.makeSubmitData = function() {
    var code = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "code"
    });
    var waybillNum = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "waybillNum"
    });
    var customerId = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "customerId"
    });
    var customerTel = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "customerTel"
    });
    var customerType = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "customerType"
    });
    var source = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "source"
    });
    var handleSystem = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "handleSystem"
    });
    var priority = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "priority"
    });
    var urgeNum = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "urgeNum"
    });
    var status = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "status"
    });
    var orderType = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "orderType"
    });
    var orderTypeName = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "orderTypeName"
    });
    var orderChilType = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "orderChilType"
    });
    var orderChilTypeName = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "orderChilTypeName"
    });
    var acceptServicer = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "acceptServicer"
    });
    var acceptServicerName = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "acceptServicerName"
    });
    var submitter = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "submitter"
    });
    var submitterName = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "submitterName"
    });
    var submitTime = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "submitTime"
    });
    var orderTitle = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "orderTitle"
    });
    var orderRemark = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "orderRemark"
    });
    var customerIdentity = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "customerIdentity"
    });
    var isReturnVisit = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "isReturnVisit"
    });
    var returnVisitType = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "returnVisitType"
    });
    var handleBranch = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "handleBranch"
    });
    var handleBranchName = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "handleBranchName"
    });
    var handleExplain = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "handleExplain"
    });
    var deadline = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "deadline"
    });
    var closingTime = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "closingTime"
    });
    var callNum = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "callNum"
    });
    var orderRecord = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "orderRecord"
    });
    var duration = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "duration"
    });
    var isTimeout = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "isTimeout"
    });
    var timeoutDuration = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "timeoutDuration"
    });
    var label = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "label"
    });
    var dutyName = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "dutyName"
    });
    var duty = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "duty"
    });
    var dutyType = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "dutyType"
    });
    var restartNum = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "restartNum"
    });
    var restartReason = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "restartReason"
    });
    var processinstId = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "processinstId"
    });
    var pid = CsOrderCustomerServiceDatagrid.grid.datagrid('getEditor', {
        index : CsOrderCustomerServiceDatagrid.editingIndex,
        field : "pid"
    });

    var data = {
        code : $(code.target).textbox("getValue"),
        waybillNum : $(waybillNum.target).textbox("getValue"),
        customerId : $(customerId.target).textbox("getValue"),
        customerTel : $(customerTel.target).textbox("getValue"),
        customerType : $(customerType.target).textbox("getValue"),
        source : $(source.target).textbox("getValue"),
        handleSystem : $(handleSystem.target).textbox("getValue"),
        priority : $(priority.target).textbox("getValue"),
        urgeNum : $(urgeNum.target).textbox("getValue"),
        status : $(status.target).textbox("getValue"),
        orderType : $(orderType.target).textbox("getValue"),
        orderTypeName : $(orderTypeName.target).textbox("getValue"),
        orderChilType : $(orderChilType.target).textbox("getValue"),
        orderChilTypeName : $(orderChilTypeName.target).textbox("getValue"),
        acceptServicer : $(acceptServicer.target).textbox("getValue"),
        acceptServicerName : $(acceptServicerName.target).textbox("getValue"),
        submitter : $(submitter.target).textbox("getValue"),
        submitterName : $(submitterName.target).textbox("getValue"),
        submitTime : $(submitTime.target).textbox("getValue"),
        orderTitle : $(orderTitle.target).textbox("getValue"),
        orderRemark : $(orderRemark.target).textbox("getValue"),
        customerIdentity : $(customerIdentity.target).textbox("getValue"),
        isReturnVisit : $(isReturnVisit.target).textbox("getValue"),
        returnVisitType : $(returnVisitType.target).textbox("getValue"),
        handleBranch : $(handleBranch.target).textbox("getValue"),
        handleBranchName : $(handleBranchName.target).textbox("getValue"),
        handleExplain : $(handleExplain.target).textbox("getValue"),
        deadline : $(deadline.target).textbox("getValue"),
        closingTime : $(closingTime.target).textbox("getValue"),
        callNum : $(callNum.target).textbox("getValue"),
        orderRecord : $(orderRecord.target).textbox("getValue"),
        duration : $(duration.target).textbox("getValue"),
        isTimeout : $(isTimeout.target).textbox("getValue"),
        timeoutDuration : $(timeoutDuration.target).textbox("getValue"),
        label : $(label.target).textbox("getValue"),
        dutyName : $(dutyName.target).textbox("getValue"),
        duty : $(duty.target).textbox("getValue"),
        dutyType : $(dutyType.target).textbox("getValue"),
        restartNum : $(restartNum.target).textbox("getValue"),
        restartReason : $(restartReason.target).textbox("getValue"),
        processinstId : $(processinstId.target).textbox("getValue"),
        pid : $(pid.target).textbox("getValue")
    };
    return data;
};

// 清空查询---------------------------------------------------------------------------------------------------
CsOrderCustomerServiceDatagrid.toolbar.find("[tag=clear]").click(function() {
    CsOrderCustomerServiceDatagrid.grid.datagrid("removeFilterRule");
    CsOrderCustomerServiceDatagrid.grid.datagrid("load", {});
});
